<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('机号')">
              <a-input :placeholder="$t('请输入机号')" v-model="queryParam.machineNo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('会员信息')">
                <a-input :placeholder="$t('请输入手机号/卡号/姓名/邮箱')" v-model="queryParam.memberInfo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('业务类型')">
                <a-select v-model="queryParam.transType">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in transTypeList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('投币种类')">
                <a-select v-model="queryParam.type">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in typeList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('投币类型')">
                <a-select v-model="queryParam.inputType">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in inputTypeList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('退票种类')">
                <a-select v-model="queryParam.outputKind">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in outputKindList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('退票类型')">
                <a-select v-model="queryParam.outputType">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in outputTypeList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备名称(编码)')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('机台名称')">
                <a-select show-search :filter-option="false" @search="gameSearch" v-model="queryParam.gameId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in gameList">
                    <span>{{ item.name }}</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.tenantId">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
              <a-button
              v-has="'BisGameCoinRecordController:export'"
                style="margin-left: 8px"
                type="primary"
                :loading="exloading"
                @click="handleExportXls($t('投币退票记录'))"
                >{{ $t('导出') }}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{ x: true, y: tableHeight }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('编辑') }}</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{ $t('更多') }} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{ $t('详情') }}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
                  <a>{{ $t('删除') }}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('投币值') }}：</span>
            <span>{{ totalInfo.coin ? totalInfo.coin.toFixed(2) : totalInfo.coin }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('退票值') }}：</span>
            <span>{{ totalInfo.outputValue ? totalInfo.outputValue.toFixed(0) : totalInfo.outputValue }}</span>
          </div>
        </template>
      </a-table>
    </div>

    <bis-game-coin-record-modal ref="modalForm" @ok="modalFormOk"></bis-game-coin-record-modal>
  </a-card>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import BisGameCoinRecordModal from './modules/BisGameCoinRecordModal'

export default {
  name: 'BisGameCoinRecordList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BisGameCoinRecordModal,
  },
  data() {
    return {
      description: '投币退票记录管理页面',
      // 表头
      columns: [
        {
          title: this.$t('时间'),
          align: 'center',
          dataIndex: 'createTime',
          width: 200,
          sorter: true,
        },
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
          width: 120,
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'chipNo',
          width: 120,
        },
        {
          title: this.$t('邮箱'),
          align: 'center',
          dataIndex: 'email',
          width: 160,
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
          width: 120,
        },
        {
          title: this.$t('机号'),
          align: 'center',
          dataIndex: 'machineNo',
          width: 140,
        },
        {
          title: this.$t('机台名称'),
          align: 'center',
          dataIndex: 'gameId_dictText',
          width: 120,
        },
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'transType_dictText',
          width: 150,
        },
        {
          title: this.$t('投币方式'),
          align: 'center',
          dataIndex: 'inputWay_dictText',
          width: 120,
        },
        {
          title: this.$t('投币种类'),
          align: 'center',
          dataIndex: 'type_dictText',
          width: 120,
        },
        {
          title: this.$t('投币类型'),
          align: 'center',
          dataIndex: 'inputType_dictText',
          width: 120,
        },
        {
          title: this.$t('投币值'),
          align: 'right',
          dataIndex: 'coin',
          width: 120,
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):text;
          }
        },
        {
          title: this.$t('退票种类'),
          align: 'center',
          dataIndex: 'outputKind_dictText',
          width: 140,
        },
        {
          title: this.$t('退票类型'),
          align: 'center',
          dataIndex: 'outputType_dictText',
          width: 140,
        },
        {
          title: this.$t('礼品名称'),
          align: 'center',
          dataIndex: 'outputGoodsId_dictText',
          width: 260,
        },
        {
          title: this.$t('距上次退投币值'),
          align: 'right',
          dataIndex: 'sumCoin',
          width: 180,
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):text;
          }
        },
        {
          title: this.$t('退票值'),
          align: 'right',
          dataIndex: 'outputValue',
          width: 140,
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(0):text;
          }
        },
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
          width: 140,
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceName',
          width: 140,
        },
      ],
      url: {
        list: '/game/bisGameCoinRecord/list',
        delete: '/game/bisGameCoinRecord/delete',
        deleteBatch: '/game/bisGameCoinRecord/deleteBatch',
        exportXlsUrl: '/game/bisGameCoinRecord/exportXls',
        importExcelUrl: 'game/bisGameCoinRecord/importExcel',
        totalUrl: 'game/bisGameCoinRecord/total',
      },
      dictOptions: {},
      superFieldList: [],
      typeList: [],
      transTypeList: [],
      inputTypeList: [],
      outputKindList: [],
      outputTypeList: [],
      deviceList: [],
      gameList: [],
      moment,
      queryParam: {
        createTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        createTime_end: moment().format('YYYY-MM-DD 23:59:59'),
        type: '',
        transType: '',
        inputType: '',
        outputKind: '',
        outputType: '',
        deviceId: '',
        gameId: '',
        tenantId: '',
      },
      isCustom: true,
      isHideData: false,
      isfooter: true,
      shops: [],
      tenantid: Vue.ls.get('TENANT_ID'),
      hideCond: 2,
    }
  },
  created() {
    if (!this.tenantid) {
      let dtl = {
          title: this.$t('门店编码'),
          align: 'center',
          dataIndex: 'tenantId',
          width: 100,
        },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'tenantId_dictText',
          width: 140,
        }
      this.columns.unshift(dtl1)
      this.columns.unshift(dtl)
    }
    this.getSuperFieldList()
  },
  mounted() {
    let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.customInit()
      initDictOptions('bis_game_coin_record.type').then((res) => {
        if (res.success) {
          this.typeList = res.result
          this.typeList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_game_coin_record.trans_type').then((res) => {
        if (res.success) {
          this.transTypeList = res.result
          this.transTypeList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_game.input_type').then((res) => {
        if (res.success) {
          this.inputTypeList = res.result
          this.inputTypeList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_game_coin_record.output_kind').then((res) => {
        if (res.success) {
          this.outputKindList = res.result
          this.outputKindList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_game.output_type').then((res) => {
        if (res.success) {
          this.outputTypeList = res.result
          this.outputTypeList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
    },
    customInit() {
      this.handleSearch('')
      this.gameSearch('')
      this.isHideData = true
      this.queryParam.dataType = 2
      this.loadData(1)
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    gameSearch(value) {
      let query = {}
      if (value) {
        query.gameInfo = value
      }
      getAction('/game/bisGame/nameList', query).then((res) => {
        if (res.success) {
          this.gameList = res.result.records
          this.gameList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset() {
      this.queryParam = {
        createTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        createTime_end: moment().format('YYYY-MM-DD 23:59:59'),
        type: '',
        transType: '',
        inputType: '',
        outputKind: '',
        outputType: '',
        deviceId: '',
        gameId: '',
        tenantId: '',
      }
      this.selectedRowIndex = []
      this.customInit()
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'int', value: 'tenantId', text: this.$t('租户id'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'gameId', text: this.$t('游乐项目ID'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'gameTypeId', text: this.$t('项目分类ID'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'deviceId', text: this.$t('设备ID'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'deviceType', text: this.$t('设备类型'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'type', text: this.$t('投币类型'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'coin', text: this.$t('投币值'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'state', text: this.$t('状态'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'outputType', text: this.$t('退票类型'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'outputValue', text: this.$t('退票值'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'outputGoodsId', text: this.$t('退礼品ID'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'inputType', text: this.$t('投币类型'), dictCode: '' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>